﻿namespace Neovolve.Toolkit.Workflow.Design
{
    using System;
    using System.Activities;
    using System.Activities.Presentation.Metadata;
    using System.Activities.Presentation.Model;
    using System.ComponentModel;
    using Neovolve.Toolkit.Workflow.Activities;
    using Neovolve.Toolkit.Workflow.Design.Presentation;

    /// <summary>
    /// The <see cref="RegisterMetadata"/>
    ///   class is used to register metadata for activity designers.
    /// </summary>
    public class RegisterMetadata : IRegisterMetadata
    {
        /// <summary>
        /// Stores the generic definition of ActivityAction`16.
        /// </summary>
        private static readonly Type _activityActionGenericType =
            typeof(
                ActivityAction
                    <Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object>).
                GetGenericTypeDefinition();

        /// <summary>
        /// Stores the generic type definition of InstanceResolver.
        /// </summary>
        private static readonly Type _instanceResolverT16GenericType =
            typeof(
                InstanceResolver
                    <Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object>).
                GetGenericTypeDefinition();

        /// <summary>
        /// Registers this instance.
        /// </summary>
        public void Register()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();

            builder.AddCustomAttributes(typeof(ExecuteBookmark), new DesignerAttribute(typeof(ExecuteBookmarkDesigner)));
            builder.AddCustomAttributes(typeof(ExecuteBookmark<>), new DesignerAttribute(typeof(ExecuteBookmarkTDesigner)));
            builder.AddCustomAttributes(typeof(GetWorkflowInstanceId), new DesignerAttribute(typeof(GetWorkflowInstanceIdDesigner)));
            builder.AddCustomAttributes(_instanceResolverT16GenericType, new DesignerAttribute(typeof(InstanceResolverDesigner)));
            builder.AddCustomAttributes(typeof(SystemFailureEvaluator), new DesignerAttribute(typeof(SystemFailureEvaluatorDesigner)));

            MetadataStore.AddAttributeTable(builder.CreateTable());

            MorphHelper.AddPropertyValueMorphHelper(_activityActionGenericType, MorphExtension.MorphActivityAction);
        }

        /// <summary>
        /// Gets the type of the instance resolver T16 generic.
        /// </summary>
        /// <value>
        /// The type of the instance resolver T16 generic.
        /// </value>
        internal static Type InstanceResolverT16GenericType
        {
            get
            {
                return _instanceResolverT16GenericType;
            }
        }
    }
}